package dto

import (
	"time"

	system "soma-server/model/system"
)

// 返回给前端的当前用户信息
type UserInfoDto struct {
	ID        uint           `json:"id"`
	CreatedAt time.Time      `json:"CreatedAt"`
	Username  string         `json:"username"`
	Sex       uint           `json:"sex"`
	Mobile    string         `json:"mobile"`
	Avatar    string         `json:"avatar"`
	Nickname  string         `json:"nickname"`
	Remark    string         `json:"remark"`
	Status    uint           `json:"status"`
	Roles     []*system.Role `json:"roles"`
}

func ToUserInfoDto(user system.User) *UserInfoDto {
	return &UserInfoDto{
		ID:        user.ID,
		CreatedAt: user.CreatedAt,
		Username:  user.Username,
		Sex:       user.Sex,
		Mobile:    user.Mobile,
		Avatar:    *user.Avatar,
		Nickname:  *user.Nickname,
		Remark:    *user.Remark,
		Status:    user.Status,
		Roles:     user.Roles,
	}
}

// 返回给前端的用户列表
type UsersDto struct {
	ID        uint      `json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	Username  string    `json:"username"`
	Mobile    string    `json:"mobile"`
	Sex       uint      `json:"sex"`
	Avatar    string    `json:"avatar"`
	Nickname  string    `json:"nickname"`
	Remark    string    `json:"remark"`
	Status    uint      `json:"status"`
	Creator   string    `json:"creator"`
	RoleIds   []uint    `json:"roleIds"`
}

func ToUsersDto(userList []*system.User) []UsersDto {
	var users []UsersDto
	for _, user := range userList {
		userDto := UsersDto{
			ID:        user.ID,
			CreatedAt: user.CreatedAt,
			Username:  user.Username,
			Mobile:    user.Mobile,
			Sex:       user.Sex,
			Avatar:    *user.Avatar,
			Nickname:  *user.Nickname,
			Remark:    *user.Remark,
			Status:    user.Status,
			Creator:   user.Creator,
		}
		roleIds := make([]uint, 0)
		for _, role := range user.Roles {
			roleIds = append(roleIds, role.ID)
		}
		userDto.RoleIds = roleIds
		users = append(users, userDto)
	}

	return users
}
